package com.ruiya.staging.goods.dataio.controller;

import com.ruiya.staging.data.core.db.SqlExecutor;
import com.ruiya.staging.goods.dataio.cfg.GoodsDbConfig;
import com.ruiya.staging.goods.dataio.cfg.TestDbConfig;
import com.ruiya.staging.goods.dataio.dao.GoodsInfoDao;
import com.ruiya.staging.goods.dataio.model.GoodsInfoDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RefreshScope
@RestController
public class TestDBContoller {
    private static final Logger _log = LoggerFactory.getLogger(TestDBContoller.class);

    //static final String DB_TEST = "mysql://root:123456@localhost:3306/testdb?serverTimezone=GMT%2B8";

    @Autowired
    private TestConfig testConfig;

    @Autowired
    private GoodsInfoDao goodsInfoDao;

    @GetMapping("/test/name")
    public String name() {
        return testConfig.getName();
    }

    @Autowired
    private GoodsDbConfig goodsDbConfig;

    @GetMapping("/test/dbconn")
    public String config() {
        return goodsDbConfig.getUrl();
    }

    @Autowired
    private TestDbConfig testDbConfig;

    @GetMapping("/testdb/users")
    public List<TestUser> users() {
        _log.info("testDbConfig.url={}", testDbConfig.getUrl());
        SqlExecutor.SqlSession session = SqlExecutor.openSession(testDbConfig.getUrl());
        try {
            List<TestUser> ret = session.selectList("SELECT * FROM users limit 3", TestUser.class);
            return ret;
        } finally {
            session.close();
        }
    }

    @GetMapping("/testdb/temp")
    public GoodsInfoDO temp(){
        GoodsInfoDO goodsInfoDTO = goodsInfoDao.getGoodsInfo(
                "0d9d937d-0d32-46ca-ad20-87b2ce5f6493",
                "049f2005-f559-44a1-92d3-1be87bccce1c");
        return goodsInfoDTO;
    }

}
